package com.atguigu.es;

import com.atguigu.beans.Employee;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.search.aggregation.MetricAggregation;
import io.searchbox.core.search.aggregation.TermsAggregation;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.List;

public class Select2 {
    public static void main(String[] args) throws IOException {
        //一.创建和es数据库的连接,获取一个客户端

        //1. new出这个工厂
        JestClientFactory factory = new JestClientFactory();
        //2. 看源码这个工厂有连接es地址的默认属性,得主动为属性赋值,否则连不上
        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();
        factory.setHttpClientConfig(httpClientConfig);
        //3.通过工厂得到jestclient
        JestClient client = factory.getObject();


        //二.中间操作
            //封装请求体

        MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("hobby", "购物");
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("emps_count").field("gender.keyword")
                .size(10).subAggregation(AggregationBuilders.avg("gender_avg_age").field("age"));

        SearchSourceBuilder source = new SearchSourceBuilder().query(matchQueryBuilder).aggregation(termsAggregationBuilder);

        //  1.传入请求体给建造的查询对象
        Search search = new Search.Builder(source.toString()).addIndex("test").addType("emps").build();
        //  2.执行查询结果
        SearchResult searchResult = client.execute(search);
        System.out.println("total:"+searchResult.getTotal());
        System.out.println("max_score:"+searchResult.getMaxScore());
        //  3.根据响应体获取相应的查询结果
        //3.1 拿取每个hit目标
        List<SearchResult.Hit<Employee, Void>> hits = searchResult.getHits(Employee.class);

        for (SearchResult.Hit<Employee, Void> hit : hits) {
            System.out.println(hit.index + "," + hit.type + hit.id + "," + hit.score);
            System.out.println(hit.source);
        }
        //3.2 拿取agg
        MetricAggregation aggregations = searchResult.getAggregations();
        TermsAggregation emps_count = aggregations.getTermsAggregation("emps_count");
        List<TermsAggregation.Entry> buckets = emps_count.getBuckets();
        for (TermsAggregation.Entry bucket : buckets) {
            System.out.println(bucket.getKey() + "," + bucket.getCount() + "," + bucket.getAvgAggregation("gender_avg_age").getAvg());
        }

        //关闭客户端
        try {
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
